package com.apchernykh.rozhlasapp.ui.podcasts;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.apchernykh.ceskyrozhlas.basic.R;

import com.apchernykh.rozhlasapp.ui.RozhlasAppBaseFragmentActivity;
import com.apchernykh.rozhlasapp.utils.ConditionalLog;
import com.apchernykh.rozhlasapp.utils.podcastsaver.RecorderFactory;
import com.apchernykh.types.DetailedUrl;
import com.apchernykh.types.Show;

public class PodcastShowListAdapter extends ArrayAdapter<Show> {
	int mLayoutId;

	public PodcastShowListAdapter(Context iContext, int iTextViewResourceId,
			List<Show> iObjects) {
		super(iContext, iTextViewResourceId, iObjects);
		mLayoutId = iTextViewResourceId;
	}

	@Override
	public View getView(int iPosition, View iCurrentView, ViewGroup iParent) {
		try {
			Show aCurrentItem = getItem(iPosition);

			View aAboutView = LayoutInflater.from(getContext()).inflate(
					mLayoutId, null);

			TextView aTitleTextView = (TextView) aAboutView
					.findViewById(R.id.show_title);
			aTitleTextView.setText(aCurrentItem.getTitle());
			TextView aDetailsTextView = (TextView) aAboutView
					.findViewById(R.id.show_description);
			aDetailsTextView.setText(aCurrentItem.getDescription());
			TextView aTimeTextView = (TextView) aAboutView
					.findViewById(R.id.show_time);
			aTimeTextView.setText(aCurrentItem.getTimeString());

			aAboutView
					.setOnClickListener(new PodcastPlaybackClickListener(aCurrentItem));

			
			ImageView aDownloadImage = (ImageView) aAboutView
					.findViewById(R.id.download_item);
			aDownloadImage
					.setOnClickListener(new PodcastDownloadClickListener(
							aCurrentItem));

			aAboutView.setOnClickListener(new PodcastPlaybackClickListener(
					aCurrentItem));

			aAboutView.setOnLongClickListener(new OnLongClickListener() {
				public boolean onLongClick(View iView) {
					ImageView downloadImage = (ImageView) iView
							.findViewById(R.id.download_item);
					int currentVisibility = downloadImage.getVisibility();
					downloadImage
							.setVisibility(currentVisibility == View.VISIBLE ? View.GONE
									: View.VISIBLE);
					return false;
				}
			});

			return aAboutView;
		} catch (Exception e) {
			ConditionalLog.d(this.toString(), "cannot build podcasts list", e);
		}
		return null;
	}

	class PodcastDownloadClickListener implements OnClickListener {
		Show mURL = null;

		public PodcastDownloadClickListener(Show iItem) {
			mURL = iItem;
		}

		public void onClick(View iView) {
			iView.setVisibility(View.GONE);
			RecorderFactory.getRecorder(iView.getContext()).startDownload(mURL, iView);
		}
	}
	
	class PodcastPlaybackClickListener implements OnClickListener {
		Show mSelectedShow = null;

		public PodcastPlaybackClickListener(Show iItem) {
			mSelectedShow = iItem;
		}

		public void onClick(View iView) {
			RozhlasAppBaseFragmentActivity.mApplicationModel.startPlayback(iView.getContext().getApplicationContext(), new DetailedUrl(mSelectedShow));
		}
	}

}